package com.example.taobaou.ui.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.taobaou.R;
import com.example.taobaou.model.domain.IBsaeInfo;
import com.example.taobaou.model.domain.SelectedContent;
import com.example.taobaou.utils.Constants;
import com.example.taobaou.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class SelectedPageRightAdapter extends RecyclerView.Adapter<SelectedPageRightAdapter.InnerHolder> {

    List<SelectedContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean>
            mData = new ArrayList<>();
    private OnSelectedPageContentItemClickListener mContentItemClickLisenter = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selected_page_content, parent, false);

        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        SelectedContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean item = mData.get(position);
        holder.setData(item);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mContentItemClickLisenter != null) {
                    mContentItemClickLisenter.onItemClickListener(item);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(SelectedContent content) {
        LogUtils.d(this, "右边——------------------------->");
        if (content.getCode() == Constants.SUCCESS_CODE || content != null) {
            List<SelectedContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> uatm_tbk_item = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
            mData.clear();
            mData.addAll(uatm_tbk_item);
            notifyDataSetChanged();

        }


    }

    public class InnerHolder extends RecyclerView.ViewHolder {


        private final ImageView cover;
        private final TextView off;
        private final TextView title;
        private final TextView original;
        private final TextView buy;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            cover = itemView.findViewById(R.id.selected_cover);
            off = itemView.findViewById(R.id.selected_off);
            title = itemView.findViewById(R.id.selected_title);
            original = itemView.findViewById(R.id.selected_original);
            buy = itemView.findViewById(R.id.buy_btn);

        }

        public void setData(SelectedContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean item) {
            title.setText(item.getTitle());
            if (TextUtils.isEmpty(item.getCoupon_click_url())) {
                original.setText("来晚啦，已经没有优惠券了！");
                buy.setVisibility(View.GONE);
            } else {
                original.setText("原价" + item.getZk_final_price());
                buy.setVisibility(View.VISIBLE);
            }

            String pict_url = item.getPict_url();
            String targetUrl = "https:" + pict_url;
            Glide.with(itemView.getContext()).load(targetUrl).into(cover);

            if (item.getCoupon_info().isEmpty()) {
                buy.setVisibility(View.GONE);
                off.setVisibility(View.GONE);
            } else {
                off.setVisibility(View.VISIBLE);
                off.setText(item.getCoupon_info());
            }

        }
    }

    public void setOnItemClickLisenter(OnSelectedPageContentItemClickListener item) {
        this.mContentItemClickLisenter = item;
    }

    public interface OnSelectedPageContentItemClickListener {
        void onItemClickListener(IBsaeInfo item);
    }
}
